package com.kexuexiong.demo.bear.controller.liteflow;

import com.kexuexiong.demo.bear.common.dto.LiteFlowDTO;
import com.kexuexiong.demo.bear.common.dto.Result;
import com.kexuexiong.demo.bear.controller.BaseController;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/lite/flow/")
@Slf4j
@Validated
@Api(tags = {"liteflow"})
public class LiteFlowController extends BaseController {

    @Autowired
    private FlowExecutor flowExecutor;

    /**
     * 执行
     *
     * @param liteFlowDTO
     * @return
     */
    @RequestMapping(value = "/executor", method = {RequestMethod.POST})
    @ApiOperation(value = "executor", notes = "executor执行")
    public Result<String> executor(@Parameter(name = "liteFlowDTO") @Valid @RequestBody LiteFlowDTO<String> liteFlowDTO) {
        log.info("executor,chainName=>{}", liteFlowDTO);
        flowExecutor.reloadRule();
        LiteflowResponse liteflowResponse = flowExecutor.execute2Resp(liteFlowDTO.getChainName(),null, liteFlowDTO);
        if (!liteflowResponse.isSuccess()) {
            return Result.fail(liteflowResponse.getMessage());
        }
        return Result.success();
    }
}
